// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
// 和后台交换的业务逻辑
let gamelogic =  cc.Class({
    properties:{

    },

    toLogin(code,appId){
        let data = {
            code:code,
            appId:appId
        }
        let loginCb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.log("登陆成功",ret);
                cc.tg.cakeRoom.token = ret.data;
                cc.tg.adDataCenter.toShowAd("kp");
            }
            else
            {
                cc.log("登陆失败",ret);
               
               
            }
        }
        cc.tg.http.sendReq("/user/subject/login/wechat",data,loginCb.bind(this));
    },
    //获取自己的数据
    toGetUserInfo(){
        let data = {
            token:cc.tg.cakeRoom.token,
        }

        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.tg.cakeRoom.initByUserData(ret.data);
                cc.tg.eventCenter.dispatchEvent("onUpdateInfo");
            }
            else
            {
                cc.log("get userInfo error ",ret);
            }
        }

        cc.tg.http.sendReq("/app/coalesce/resource",data,cb.bind(this));
    },

    //获取金币，签到，可提取金额等信息
    toGetAccInfo(){
        let data = {
            token:cc.tg.cakeRoom.token,
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.tg.cakeRoom.initAccInfo(ret.data);
            }
            else
            {
                cc.log("get acc info fail",ret);
            }
        }

        cc.tg.http.sendReq("/user/account/info",data,cb.bind(this));
    },
    //签到
    toSign(){
        let data = {
            token:cc.tg.cakeRoom.token
        }
        let signCb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.log("签到成功");
                cc.tg.cakeRoom.accContinuousSign++;
                cc.tg.eventCenter.dispatchEvent("updateSign")
            }
            else
            {
                cc.log("签到失败");
            }
        }
        cc.tg.http.sendReq("/user/account/sign",data,signCb.bind(this))
    },
    //购买蛋糕
    toBuyItem(itemCode){
        let data = {
            token:cc.tg.cakeRoom.token,
            itemCode:itemCode
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.log("buy item sucess");
                cc.tg.cakeRoom.onBuyItemSucess(ret.data,itemCode)
            }
            else{
                cc.log("buy item fail",ret);
            }
        }

        cc.tg.http.sendReq("/app/coalesce/buy",data,cb.bind(this));
    },
    //升级
    toUpdateLevel(level,sPos,tPos){
        let data  = {
            level:level,
            token:cc.tg.cakeRoom.token
        };
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.log("update sucess");
                cc.tg.cakeRoom.onUpdateSucess(ret.data,sPos,tPos);
            }
            else
            {
                cc.log("update fail",ret);
            }
        }
        cc.tg.http.sendReq("/app/coalesce/blend",data,cb.bind(this));
    },
    //回收
    toResolve(pos){
        let itemCode = cc.tg.cakeRoom.cakePosList[pos]
        let data = {
            itemCode:itemCode,
            token:cc.tg.cakeRoom.token,

        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.log("回收成功");
                cc.tg.cakeRoom.onResolveSucess(ret.data,pos)
            }
            else{
                cc.log("回收失败",ret);
            }
        }

        cc.tg.http.sendReq("/app/coalesce/resolve",data,cb.bind(this));
    },
    //提现到微信
    txToWx(num){
        let data = {
            token:cc.tg.cakeRoom.token,
            extract:num
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.log("提现成功");
            }
            else
            {
                cc.log("提现失败",ret);
            }
        }
        cc.tg.http.sendReq("/user/account/extract",data,cb.bind(this));
    },
    //合成分红蛋糕
    toblend(itemCode){
        let data = {
            token:cc.tg.cakeRoom.token,
            itemCode:itemCode,
        }

        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.log("合并成功");
            }
            else
            {
                cc.log("合并失败",ret);
            }
        }

        cc.tg.http.sendReq("/app/coalesce/blend/formula",data,cb.bind(this));
    },
    // 获取推广码
    toGetInviteCode(){
        let data = {
            token:cc.tg.cakeRoom.token,
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.tg.cakeRoom.invicationCode = ret.data;
                cc.tg.eventCenter.dispatchEvent("onGetInviteCode");
            }   
            else
            {
                cc.log("获取推广码失败",ret);
            }
        }
        cc.tg.http.sendReq("/user/toPromote/invicationCode",data,cb.bind(this))
    },
    // 获取刮刮卡排行榜数据
    toRefreshRank(){
        let data = {
            token:cc.tg.cakeRoom.token,
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.log("get rank sucess");
                cc.tg.eventCenter.dispatchEvent("onRefreshRank",ret.data);
            }
            else
            {
                cc.log("get rank fail",ret);
            }
        }

       // cc.tg.http.sendReq("/scratchCard/findWinningPeople",data,cb.bind(this));
    },
    toRefreshGGKRank(){
        let data = {
            token:cc.tg.cakeRoom.token,
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.log("get rank sucess");
                cc.tg.eventCenter.dispatchEvent("onRefreshGGKRank",ret.data);
            }
            else
            {
                cc.log("get rank fail",ret);
            }
        }

        cc.tg.http.sendReq("/scratchCard/findWinningPeople",data,cb.bind(this));
    },
    // 金币开红包
    toGetHB(){
        let data = {
            token:cc.tg.cakeRoom.token,
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.log("on get Hb sucess");
                cc.tg.eventCenter.dispatchEvent("onGetHB",ret.data);
            }
            else
            {
                cc.log("on get hb fail",ret);
            }
        }

        cc.tg.http.sendReq("/user/gold/openRedPacket",data,cb.bind(this));
    },
    // 获取系统随机赠送的金币
    toGetRandomCoin(){
        let data = {
            token:cc.tg.cakeRoom.token,
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.tg.eventCenter.dispatchEvent("showCoin",ret.data)
            }
            else
            {
                cc.log("get list solt fail",ret);
            }
        }

        cc.tg.http.sendReq("/user/gold/listForGoldSolt",data,cb.bind(this));
    },

    //获取金币槽中的金币
    toGetCoin(slot){
        let data = {
            slot:slot,
            token:cc.tg.cakeRoom.token,
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0 )
            {

            }
            else
            {
                cc.log("get slot coin fail",slot);
            }
        }
        cc.tg.http.sendReq("//user/gold/getGold",data,cb.bind(this));
    },
    // 获取刮刮卡界面
    toGetGGKList(){
        let data = {
            token:cc.tg.cakeRoom.token,
        }

        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.tg.eventCenter.dispatchEvent("onRefreshGGK",ret.data)
            }
            else
            {
                cc.log("get ggk list fail",ret);
            }
        }
        cc.tg.http.sendReq("/scratchCard/list",data,cb.bind(this));
    },
    // 使用刮刮卡
    toUseGGk(cardId){
        let data = {
            token:cc.tg.cakeRoom.token,
            cardId:cardId,
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.tg.eventCenter.dispatchEvent("onUseGGk",ret.data)
            }
            else
            {
                cc.log("use ggk error");
            }
        }
        cc.tg.http.sendReq("/scratchCard/useCard",data,cb.bind(this));
    },
    //获取刮刮卡剩余次数
    toGetluckyWheel(){
        let data = {
            token:cc.tg.cakeRoom.token,
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {
                cc.tg.cakeRoom.unuseCount = ret.data.unusedNumber;
                cc.tg.eventCenter.dispatchEvent("onGetGGKCount",ret.data.unusedNumber)
            }
            else
            {
                cc.log("get ggk count fail ",ret);
            }
        }

        cc.tg.http.sendReq("/luckyWheel/enter",data,cb.bind(this))
    },
    //转大转盘
    toSpin(){
        let data = {
            token:cc.tg.cakeRoom.token,
        }
        let cb = (ret)=>{
            if(ret && ret.code == 0)
            {

                cc.tg.eventCenter.dispatchEvent("onSpinResult",ret.data);
            }
            else
            {
                cc.log(ret.msg);
            }
        }

        cc.tg.http.sendReq("/luckyWheel/luckyDrow",data,cb.bind(this))
    }

});

module.exports = gamelogic;
